/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nightonke.boommenusample.slice;

import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenu.ButtonEnum;
import com.nightonke.boommenusample.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class ToolBarAbilitySlice extends AbilitySlice {

    private BoomMenuButton bmb;
    private ToolBar toolBar;

    /**
     * onStart
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer cc = initView();
        super.setUIContent(cc);

    }

    private ComponentContainer initView() {

        DirectionalLayout myLayout = new DirectionalLayout(this);
        myLayout.setWidth(MATCH_PARENT);
        myLayout.setHeight(MATCH_PARENT);
        myLayout.setOrientation(Component.VERTICAL);

        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(element);

        myLayout.addComponent(getToolBar());
        return myLayout;

    }

    private ToolBar getToolBar() {
        if (toolBar == null) {
            toolBar = new ToolBar(getContext());
            toolBar.setLayoutConfig(new DirectionalLayout.LayoutConfig(MATCH_PARENT, Utils.vpToPx(getContext(), 56)));


            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(63,81,180));
            toolBar.setBackground(element);

            toolBar.setTitleTextColor(Color.WHITE, Color.WHITE);
            toolBar.setTitle(getString(ResourceTable.String_app_name));


            toolBar.setMenuVisibility(Component.HIDE);
            toolBar.setOtherImageElement(new VectorElement(getContext(), ResourceTable.Graphic_ic_gallery_view));
            toolBar.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    bmb = (BoomMenuButton) findComponentById(ResourceTable.Id_bmb);
                    assert bmb != null;
                    bmb.setButtonEnum(ButtonEnum.Ham);
                    for (int i = 0; i < bmb.getPiecePlaceEnum().pieceNumber(); i++)
                        bmb.addBuilder(BuilderManager.getHamButtonBuilderWithDifferentPieceColor());
                    bmb.buildButton();
                }
            });
        }
        return toolBar;
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
